﻿using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Panda.UI.Runtime
{
    [Serializable]
    public class LayeredPageEntry
    {
        public string layerName;
        public BasePage[] pages = Array.Empty<BasePage>();
        public RectTransform layerContainer;
    }
    
    public class UIMgr : MonoBehaviour
    {
        // [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
        // static void Init()
        // {
        //     _router = null;
        // }

        // private static IRouter _router = null;

        // public static IRouter Router
        // {
        //     get
        //     {
        //         if (_router != null)
        //         {
        //             return _router;
        //         }

        //         _router = new DefaultRouter();
        //         return _router;
        //     }
        // }

        public IRouter Router { get; } = new DefaultRouter();

        [SerializeField] private bool dynamicSpawn = false;
        [SerializeField] private LayeredPageEntry[] layeredPageEntry = Array.Empty<LayeredPageEntry>();
        [SerializeField] private bool disablePagesOnAwake = true;
        [SerializeField] private bool openFirstPageOnStart = true;

        [SerializeField] private Camera uiCamera;
        [SerializeField] private Canvas mainCanvas;

        public Camera UICamera => uiCamera;
        public LayeredPageEntry[] LayeredPageEntry => layeredPageEntry;
        public Canvas MainCanvas => mainCanvas;


        private void Awake()
        {
            if (!uiCamera)
            {
                uiCamera = Camera.main;
                mainCanvas.worldCamera = uiCamera;
            }
            
            if (disablePagesOnAwake && !dynamicSpawn)
            {
                for (var i = 0; i < layeredPageEntry.Length; i++)
                {
                    var item = layeredPageEntry[i];
                    for (var j = 0; j < item.pages.Length; j++)
                    {
                        var page = item.pages[j];
                        page.gameObject.SetActive(false);
                    }
                }
            }
        }

        private void Start()
        {
            for (var i = 0; i < layeredPageEntry.Length; i++)
            {
                var item = layeredPageEntry[i];
                Router.Register(item.layerName, new DefaultLayer());

                var layerContainerRt = item.layerContainer != null ? item.layerContainer : transform as RectTransform;
                for (var j = 0; j < item.pages.Length; j++)
                {
                    var page = item.pages[j];
                    
                    var instPage = page;
                    if (dynamicSpawn)
                    {
                        instPage = Object.Instantiate(page, layerContainerRt, false);
                        instPage.name = page.name;
                        if (disablePagesOnAwake)
                        {
                            instPage.gameObject.SetActive(false);
                        }
                    }
                    
                    Router.Register(instPage.name, instPage, item.layerName);
                }
            }

            if (openFirstPageOnStart)
            {
                Router.NavigateTo(layeredPageEntry[0].pages[0].name, null, null, null, null, false, layeredPageEntry[0].layerName);
            }
        }

        public T GetCurrentWindow<T>() where T : class, IPage
        {
            return Router.GetCurrentPage() as T;
        }

        public void CloseWindow(string screenId)
        {
        }
        
        public void OpenWindow(string screenId)
        {
            Router.NavigateTo(screenId, null, null, null, null, false, null);
        }
        
        public void OpenWindow(string screenId, object data)
        {
            Router.NavigateTo(screenId, data, null, null, null, false, null);
        }
    }
}